package jp.sourceforge.akjrcp.videotools.muxsubtitle;


import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer;
import jp.sourceforge.akjrcp.dropsomething.DropsomethingView;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegFLVFileEffector;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.part.ViewPart;

/**
 * may be this class is underconstraction
 * 
 *
 */
public class MuxSubtitleView extends ViewPart {
	public static Log log=LogFactory.getLog(MuxSubtitleView.class);
	
	public static final String ID = MuxSubtitleView.class.getName();

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	
	Composite composite;

	private FileDirChoicer inputFileChooser;

	private FileDirChoicer srtFileChooser;

	private FileDirChoicer outputFileChooser;
	
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(1,false));
		
		inputFileChooser = new FileDirChoicer(parent,SWT.BORDER,FileDirChoicer.FILE,"Input File");
		inputFileChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		srtFileChooser = new FileDirChoicer(parent,SWT.BORDER,FileDirChoicer.FILE,"Srt File");
		srtFileChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		outputFileChooser = new FileDirChoicer(parent,SWT.BORDER,FileDirChoicer.SAVE,"output File");
		outputFileChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		
		inputFileChooser.setFilePath("c:\\tmp\\happy.flv");
		outputFileChooser.setFilePath("c:\\tmp\\output.flv");
		Button exec=new Button(parent,SWT.NULL);
		exec.setText("exec");
		exec.addSelectionListener(new MuxSrt());
		
		
		
	}
	
	public void muxSubtitle(){
		String ffmpegPath=FFMpegUI.getFFmpeg();
		if(ffmpegPath==null){
			setStatusValue("ffmpeg.exe not found");
			return;
		}
		SubtitleFLVFileEffector effector=new SubtitleFLVFileEffector(ffmpegPath,inputFileChooser.getFile(),outputFileChooser.getFile());
		
		effector.setInputImageType(FFMpegFLVFileEffector.IMAGE_JPEG);
		effector.setOutputImageType(FFMpegFLVFileEffector.IMAGE_JPEG);
		effector.start();//maybe ok.
		
		
		long starttime=System.currentTimeMillis();
		while(!effector.isDoneRead() || !effector.isDoneWrite()){
			
			try {
				Thread.sleep(1000*3);
				setStatusValue(FFMpegFLVFileEffector.doneFrames+"frames done.it's about "+(FFMpegFLVFileEffector.doneFrames/30)+"second");
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
		}
		log.info("***finally finished***");
		long end=System.currentTimeMillis()-starttime;
		String min=""+(end/1000/60);
		log.info("time:"+min+" min");
		try {
			Thread.sleep(1000*3);
		} catch (InterruptedException e) {
		
			e.printStackTrace();
		}
	}
	
	public void setStatusValue(String value){
		PlatformUI.getWorkbench().getDisplay().asyncExec(new SetMessage(value));
		
	}
	
	public class SetMessage implements Runnable{
		String message;
		SetMessage(String message){
			this.message=message;
		}
		public void run(){
			WorkbenchWindow workbenchWindow =  (WorkbenchWindow) DropsomethingView.viewSite.getWorkbenchWindow();
			IActionBars actionBars = workbenchWindow.getActionBars();
			IStatusLineManager manager = actionBars.getStatusLineManager();
			manager.setMessage(message);
		}
	}
	
	
	public class MuxSrt implements SelectionListener{

		public void widgetDefaultSelected(SelectionEvent e) {
			
		}

		public void widgetSelected(SelectionEvent e) {
			muxSubtitle();
		}
		
	}

	
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}
	
}